package com.wave.wavesystem.serve.controller;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.wave.common.enums.ErrorCode;
import com.wave.common.model.DeleteRequest;
import com.wave.common.model.Result;
import com.wave.common.util.Func;
import com.wave.wavesystem.serve.pojo.entity.UserRole;
import com.wave.wavesystem.serve.pojo.query.UserRoleQuery;
import com.wave.wavesystem.serve.service.IUserRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/userRole")
@RequiredArgsConstructor
@Tag(name = "用户角色接口-UserRoleController")
public class UserRoleController {

    private final IUserRoleService service;

    @PostMapping("/save")
    @Operation(summary = "新增")
    public Result<UserRole> save(@RequestBody UserRole entity) {
        service.save(entity);
        return Result.success(entity);
    }

    @PostMapping("/delete")
    @Operation(summary = "通过 ids 删除")
    public Result<Void> delete(@RequestBody DeleteRequest deleteRequest) {
        if (Func.isEmpty(deleteRequest.getIds())) {
            return Result.fail(ErrorCode.NEED_PARAMS);
        }
        List<Long> removeIds = Func.strToLongList(deleteRequest.getIds());
        service.removeByIds(removeIds);
        return Result.success();
    }

    @PostMapping("/modify")
    @Operation(summary = "修改")
    public Result<UserRole> modify(@RequestBody UserRole entity) {
        service.updateById(entity);
        return Result.success(entity);
    }

    @PostMapping("/query")
    @Operation(summary = "根据条件查询")
    public Result<UserRole> query(@RequestBody UserRoleQuery query) {
        UserRole entity = service.getById(query.getId());
        return Result.success(entity);
    }

    @PostMapping("/list")
    @Operation(summary = "列表")
    public Result<List<UserRole>> list(@RequestBody UserRoleQuery query) {
        QueryChain<UserRole> queryChain = service.getQueryChain(query);
        List<UserRole> list = service.list(queryChain);
        return Result.success(list);
    }

    @PostMapping("/page")
    @Operation(summary = "分页")
    public Result<Page<UserRole>> page(@RequestBody UserRoleQuery query) {
        QueryChain<UserRole> queryChain = service.getQueryChain(query);
        Page<UserRole> page = service.page(new Page<>(query.getPageNum(), query.getPageSize()), queryChain);
        return Result.success(page);
    }

}
